package com.jiangyao.controller;


import com.jiangyao.common.ApiResult;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.model.dto.EditDeliverDTO;
import com.jiangyao.model.dto.ResumeSendDTO;
import com.jiangyao.model.entity.Resume;
import com.jiangyao.model.entity.ResumeSend;
import com.jiangyao.service.ResumeSendService;
import com.jiangyao.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.DozerBeanMapper;
import org.dozer.MappingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jiangyao.controller.BaseController;

import java.util.Date;

/**
 * <p>
 * 简历投递（求职） 前端控制器
 * </p>
 *
 * @author zhoushuangxiang
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/resumeSend")
@Api(tags = "投递简历相关接口")
public class ResumeSendController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResumeController.class);

    @Autowired
    private DozerBeanMapper dozerBeanMapper;

    @Autowired
    private ResumeSendService resumeSendService;

    @Autowired
    private ResumeService resumeService;

    /**
     * 投递简历
     * @param resumeSendDTO
     * @return
     */
    @PostMapping("/sendResume")
    @ApiOperation(value = "投递简历")
    public ApiResult sendResume(@Validated({ResumeSendDTO.Add.class}) @RequestBody ResumeSendDTO resumeSendDTO){
        try {
            Resume resume = resumeService.getResume();
            if(resume ==null){
                return ApiResult.error(501,"请先创建简历");
            }
            resume.setResumeId(null).setResumeType(2).setOrgId(resumeSendDTO.getOrgId());
            Resume kzResume = resumeService.addResume(resume);
            ResumeSend resumeSend = dozerBeanMapper.map(resumeSendDTO, ResumeSend.class);
            resumeSend.setResumeId(kzResume.getResumeId());
            resumeSend.setCreateTime(new Date());
            resumeSend.setStatus(1);
            resumeSend.setDeliveryStatus(0);
            resumeSendService.send(resumeSend);
            return ApiResult.success(resumeSend);

        } catch (ServerException e) {
            return ApiResult.error(e.getCode(),e.getMessage());
        }
    }
    @PostMapping("/sendMessage")
    @ApiOperation(value="发送通知")
    public ApiResult<Boolean> sendMessage(@RequestBody ResumeSendDTO resumeSendDTO){
        try {
            return  ApiResult.success(resumeSendService.sendMessage(resumeSendDTO));
        }catch (ServerException e){
            return ApiResult.error(e.getCode(),e.getMessage());
        }

    }


    /**
     * 修改面试邀请状态 /接受或拒绝
     * @param editDeliverDTO
     * @return
     */
    @PostMapping("/editStatus")
    @ApiOperation(value = "修改的邀面试状态 0为拒绝  2为接受")
    public ApiResult editDeliverStatus(@RequestBody EditDeliverDTO editDeliverDTO){
        try {
            resumeSendService.editStatus(editDeliverDTO);
            return ApiResult.success("修改状态成功");
        } catch (ServerException e) {
            return ApiResult.error("修改状态失败");
        }
    }
}

